package gt.actions;

import gt.domain.User;
import gt.service.UserService;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@WebServlet("/alterPassword")
public class alterPassword extends HttpServlet {

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		    String oldpassword = (String)req.getParameter("oldpassword");
		    String newpassword = (String)req.getParameter("newpassword");
		    String confirmpassword = (String)req.getParameter("confirmpassword");
		    System.out.println(oldpassword+""+newpassword+""+confirmpassword);
		    UserService userService = new UserService();
		    User user=(User) req.getSession().getAttribute("admin");
		    String id=user.getId()+"";
		    if(user.getPassword().equals(oldpassword)){
		    	boolean b=userService.alterPassword(id,newpassword);
		    	if(b){
		    		req.getRequestDispatcher("systemset/alterpwdOk.jsp").forward(req, resp);
		    	}else{
		    		req.getRequestDispatcher("systemset/alterpwdError.jsp").forward(req, resp);
		    	}
		    }else{
		    	    req.getRequestDispatcher("systemset/alterpwdError.jsp").forward(req, resp);
		    }
		    
	}

	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		this.doGet(req, resp);
	}
	
}
